package mysql

import (
	"gorm.io/gorm"
	"video-hub/videorpc/videorpc"
)

type Comment struct {
	gorm.Model
	VideoId        int64  `gorm:"type:int(11);not null;comment:视频ID"`
	UserId         int64  `gorm:"type:int(11);not null;comment:上传用户ID"`
	CommentContent string `gorm:"type:varchar(500);not null;comment:评论内容"`
	IsAnonymous    int64  `gorm:"type:tinyint;not null;default:0;comment:是否匿名0-非匿名，1-匿名"`
	CommentStatus  int64  `gorm:"type:int(11);not null;default:0;comment:评论状态0-有效，1-违规（含敏感词）"`
	ReplyId        int64  `gorm:"type:int(11);comment:回复ID"`
}

func (c *Comment) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Comment) FindUserIdAndCommentIdDel(db *gorm.DB, id int64, id2 int64) error {
	return db.Debug().Model(&Comment{}).Where("user_id =? and id =?", id, id2).Delete(c).Error
}

func (c *Comment) FindUserIdAndCommentId(db *gorm.DB, id int64, id2 int64) error {
	return db.Debug().Where("user_id =? and id =?", id, id2).First(c).Error
}

/*
SELECT
  users.nick_name username,
  comments.comment_content,
  comments.created_at
FROM
  comments
  LEFT JOIN users ON comments.user_id = users.id
*/

func CommentList(db *gorm.DB, in *videorpc.CommentListRequest) []*videorpc.CommentList {
	var list []*videorpc.CommentList
	db.Debug().Model(&Comment{}).
		Select(
			"users.nick_name username",
			"comments.comment_content",
			"comments.created_at",
		).
		Joins("LEFT JOIN users ON comments.user_id = users.id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Where("comments.video_id =?", in.VideoId).
		Order("comments.created_at desc").
		Find(&list)
	return list

}
